﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using COFE.Core.Lister;
using COFE.Core.Utils;

namespace COFE.IO.FSIEX
{
    public class FileSystemExDirectoryController : DirectoryControllerBase<FileSystemInfoEx>
    {
        #region Constructor

        //private void copyDirectory(DirectoryInfo sourceDir, DirectoryInfo targetDir)
        //{
        //    DirectoryInfo targetSourceDir = targetDir.CreateSubdirectory(sourceDir.Name);
        //    foreach (var entries in targetDir.GetFileSystemInfos())
        //    {
        //        string destName = Path.Combine(targetSourceDir.FullName, entries.Name);
        //        if (entries is FileInfo)
        //            File.Copy(entries.FullName, destName);
        //        else copyDirectory(entries as DirectoryInfo, targetSourceDir);
        //    }
        //}

        public FileSystemExDirectoryController(FileSystemExDirectoryLister directoryLister)
            : base(directoryLister)
        {
            _directoryLister = directoryLister;

            _createDirectory = (parent, name) => 
            { 
                return (parent as DirectoryInfoEx).CreateDirectory(name); 
            };
            _createFile = (parent, name) =>
            {
                var fi = new FileInfoEx(Path.Combine(parent.FullName, name));
                using (var _ = fi.Create()) { };
                return fi;
            };

            _copy = (item, taretDir) =>
            {
                if (item is FileInfoEx) File.Copy(item.FullName, Path.Combine(taretDir.FullName, item.Name));
                else
                    DirectoryEx.Copy((item as DirectoryInfoEx).FullName, (taretDir as DirectoryInfoEx).FullName);
                    //copyDirectory(item as DirectoryInfoEx, taretDir as DirectoryInfoEx);
            };
            _move = (item, taretDir) =>
            {
                string destName = PathEx.Combine(taretDir.FullName, item.Name);
                if (item is FileInfoEx) FileEx.Move(item.FullName, destName);
                else DirectoryEx.Move(item.FullName, destName);
            };

            _rename = (item, newName) =>
            {
                string source = item.FullName;
                string dest = Path.Combine(Path.GetDirectoryName(item.FullName), newName);
                if (File.Exists(source))
                    File.Move(source, dest);
                else Directory.Move(source, dest);
            };

            _delete = (item) =>
            {
                if (item is DirectoryInfoEx)
                    DirectoryEx.Delete(item.FullName);
                else FileEx.Delete(item.FullName);
            };


            _watcherListener = new ExSystemWatcherListener(_directoryLister, this);
            
        }

        #endregion

        #region Methods


        #endregion

        #region Data

        
        new FileSystemExDirectoryLister _directoryLister;
        ExSystemWatcherListener _watcherListener;

        #endregion

        #region Public Properties

        #endregion
    }
}
